layui.use(["customUtil"], function () {
    var form = layui.form;
    var util = layui.customUtil;
    var layer = layui.layer;
    //自定义验证规则
    form.verify({
        title: function (value) {
            if (!value || !value.trim()) {
                return '请输入用户名';
            }
        },
        password: function (value) {
            if (!value || !value.trim()) {
                return '请输入密码';
            }
        },
    });
    form.render();
    var login = {
        _constans: {
            api: {
                getImageVerifyCode: appCtx + "/captcha/getImageVerifyCode",
                loginVerify: appCtx + "/loginVerify"
            }
        },
        _init: function () {
            var _this = this;
            _this.bindEvent();
        },
        bindEvent: function () {
            var _this = this;
            $("#loginSubmitBtn").on("click", function (event) {
                _this.beforeSubmitLogin();
            });
            $("#refreshCaptchaId").on("click", function (event) {
                _this.refreshCaptcha();
            });
        },
        loadingCaptcha: function () {
            var _this = this;
            util.get(_this._constans.api.getImageVerifyCode, function (data) {
                $("#mainImageSrc").attr('src', 'data:image/png;base64,' + data.bigImage);
                $("#moveImageSrc").attr('src', 'data:image/png;base64,' + data.smallImage);
                $("#captcha-sign").val(data.sign);
                _this.initCaptchaIndex();
                $("#captcha_div").show();
            });
        },

        initCaptchaIndex: function () {
            var _this = this;
            $('#captcha').sliderCaptcha({
                onMouseDragEnd: function (element, dragData) {
                    $("#moveLength").val(dragData.moveLength),
                        $("#canvasLength").val(dragData.canvasLength),
                        $("#captcha_div").hide();
                    _this.submitLogin();
                }
            });
        },

        beforeSubmitLogin: function () {
            var _this = this;
            if ("true" === $("#isNeedCaptcha").val()) {
                _this.loadingCaptcha();
            } else {
                _this.submitLogin();
            }
        },

        refreshCaptcha: function () {
            var _this = this;
            _this.loadingCaptcha();
        },

        submitLogin: function () {
            var _this = this;
            let aesKey = CryptoJS.enc.Utf8.parse($("#aesKey").val());
            let encryptedData = CryptoJS.AES.encrypt($("#login-password").val(), aesKey, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7
            });

            let data = {
                username: $("#login-username").val(),
                password: encryptedData.ciphertext.toString(),
                token: $("#token").val(),
                moveLength: $("#moveLength").val(),
                canvasLength: $("#canvasLength").val(),
                sign: $("#captcha-sign").val()
            };
            util.post(_this._constans.api.loginVerify, data, function (data) {
                if (data.success) {
                    location.href = data.result.redirectUrl;
                } else {
                    layer.alert(data.messages, {
                        title: '登录失败'
                    })
                }
            });
        }
    };

    login._init();

});

